<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\tools\Qiniu;

class ImgController extends BaseController
{
    public function actionUploadImg(){
        if(isset($_FILES["file"])) {
          $file_name = $_FILES["file"]["name"];
          $new_file_name= rand(1,9999).'_'.$file_name;
          $tmp_path = $_FILES["file"]["tmp_name"];
          $intput_path = './public/upload/'.$new_file_name;
          $flag = move_uploaded_file($tmp_path, $intput_path);
          if ($flag) {
            $res = Qiniu::putFileToQiniu($intput_path);
            return Re::data(['data' => $res['domain'].$res['key']]);
          } else {
            return Re::data(null, '文件上传失败', Re::ERROR);
          }
        }
        return Re::data(null, '文件上传失败', Re::ERROR);
    }

    public function actionFetchImg(){
        $img_url = Yii::$app->request->post('img_url', '');
        $img_content = file_get_contents($img_url);
        if ($img_content) {//转为本地图片后上传七牛
          $img_path = rand(1,9999).'_wangeditor_img.png';
          file_put_contents($img_path, $img_content);
          $res = Qiniu::putFileToQiniu($img_path);
          return Re::data(['data' => $res['domain'].$res['key']]);
        }   
    }
}
